//package com.oauth.resource.config.feign;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.secure.bean.SecureRequest;
//import com.secure.constant.KeyConstant;
//import com.secure.util.RsaUtil;
//import feign.RequestTemplate;
//import feign.codec.EncodeException;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.ObjectFactory;
//import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
//import org.springframework.cloud.openfeign.support.SpringEncoder;
//
//import java.lang.reflect.Type;
//import java.time.Instant;
//import java.util.TreeMap;
//
//@Slf4j
//public class FeignBodyEncoder extends SpringEncoder {
//
//    private final ObjectMapper objectMapper;
//
//    public FeignBodyEncoder(ObjectFactory<HttpMessageConverters> messageConverters, ObjectMapper objectMapper) {
//        super(messageConverters);
//        this.objectMapper = objectMapper;
//    }
//
//    /**
//     * 请求体编码处理
//     *
//     * @param requestBody 请求体.
//     * @param bodyType    body类型
//     * @param request     请求模板
//     * @throws EncodeException
//     */
//    @Override
//    public void encode(Object requestBody, Type bodyType, RequestTemplate request) throws EncodeException {
//        TreeMap<String, String> plainMap;
//        try {
//            plainMap = objectMapper.readValue(objectMapper.writeValueAsString(requestBody), new TypeReference<TreeMap<String, String>>() {{
//            }});
//        } catch (JsonProcessingException e) {
//            throw new RuntimeException(e);
//        }
//        String sign = RsaUtil.sign(plainMap, KeyConstant.RSA_DEFAULT_PRIVATE_KEY);
//        plainMap.put("signature", sign);
//        String encryptText = RsaUtil.encryptByPublicKey(plainMap, KeyConstant.RSA_DEFAULT_PUBLIC_KEY);
//        SecureRequest secureRequest = new SecureRequest();
//        secureRequest.setAppId("user1");
//        secureRequest.setTimestamp(Instant.now().getEpochSecond());
//        secureRequest.setData(encryptText);
//        super.encode(secureRequest, SecureRequest.class, request);
//    }
//}
